﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business;
using NHibernate;
using ZLCMS.Data.Model;
using NHibernate.Criterion;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Util;
using ZLCMS.Business.TranferParam;
using Newtonsoft.Json;
using System.Text;
using DotNet.Common.TransferParam;

namespace ZLCMS.Web.SystemManage.Weixin
{
    public partial class EditQuestionItem : System.Web.UI.Page
    {
        protected FrameHelper _framePage;
        
        protected WxGameQuestionsItem _itemDB;

        protected void Page_Load(object sender, EventArgs e)
        {
            //初始化DWZ控件
            _framePage = new FrameHelper(this);

            //初始化全局变量值
            InitializeGlobalValue();

            //初始化控件值
            InitializeContent();

            if (IsPostBack)
            {
                Save();
            }
        }

        protected void InitializeContent()
        {
            //页面控件数据

            //页面控件值
            if (!IsPostBack)
            {
                BindItemType(ddl_type);
                
                if (_itemDB != null)
                {
                    tb_title.Text = _itemDB.Title;
                    tb_answer.Text = _itemDB.AnswerData;
                    tb_order.Text = _itemDB.ShowOrder.ToString();
                }
                else
                {
                    tb_order.Text = "99";
                }
            }
        }
        protected void InitializeGlobalValue()
        {
            string uid;

            try
            {
                if (!IsPostBack)
                {

                    if (string.IsNullOrWhiteSpace(Request["uid"]))
                    {
                        if (string.IsNullOrWhiteSpace(Request["gid"]))
                        {
                            throw new Exception("找不到对应信息.");
                        }

                        _gid.Text = Request["gid"];

                        _uid.Text = null;
                        return;
                    }
                    else
                    {
                        uid = Request["uid"];
                        _uid.Text = uid;
                    }
                }
                else
                {
                    if (string.IsNullOrWhiteSpace(_uid.Text))
                    {
                        return;
                    }
                    uid = _uid.Text;
                }

                using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    _itemDB = session.CreateCriteria<WxGameQuestionsItem>().Add(Expression.Eq("Id", int.Parse(uid))).UniqueResult<WxGameQuestionsItem>();
                }

                if (_itemDB == null)
                {
                    throw new Exception("找不到对应信息.");
                }
            }
            catch (System.Exception ex)
            {
                _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                return;
            }
        }
        
        protected void Save()
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                try
                {
                    if (_itemDB == null)
                    {
                        _itemDB = new WxGameQuestionsItem();

                        _itemDB.ObjectId = int.Parse(_gid.Text);
                        _itemDB.Title = tb_title.Text;
                        _itemDB.OptionData = "";
                        _itemDB.AnswerData = tb_answer.Text;
                        _itemDB.QuestionType = ddl_type.SelectedValue;
                        _itemDB.ShowOrder = int.Parse(tb_order.Text);
                        _itemDB.Point = 0;

                        session.Save(_itemDB);
                    }
                    else
                    {
                        _itemDB.ObjectId = int.Parse(_gid.Text);
                        _itemDB.Title = tb_title.Text;
                        _itemDB.OptionData = "";
                        _itemDB.AnswerData = tb_answer.Text;
                        _itemDB.QuestionType = ddl_type.SelectedValue;
                        _itemDB.ShowOrder = int.Parse(tb_order.Text);
                        _itemDB.Point = 0;

                        session.Update(_itemDB);
                    }

                    session.Flush();
                }
                catch (System.Exception ex)
                {
                    _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                    return;
                }
            }
            // 刷新 editparamgroup 页面
            _framePage.Alert(new ReturnCode()
            {
                statusCode = 200,
                message = "操作成功!",
                callbackType = "closeCurrent",
                navTabId = "editQuestionGame"
            });
        }

        private void BindItemType(DropDownList list)
        {
            list.Items.Add(new ListItem() { Text = "多选框", Value = "cbox" });
            list.Items.Add(new ListItem() { Text = "下拉框", Value = "ddlist" });
            list.Items.Add(new ListItem() { Text = "单选框", Value = "rbox" });
        }
    }
}
